<%@ page import="com.xyzboom.utils.WebUtils" %>
<%@ page import="com.xyzboom.database.entity.Course" %>
<%@ page import="java.util.List" %>
<%@ page import="com.xyzboom.database.dao.CourseDAO" %>
<%@ page import="com.xyzboom.utils.Page" %>
<%@ page import="com.xyzboom.database.entity.exception.NotEntityClassException" %>
<%@ page import="com.xyzboom.database.entity.exception.EntityColumnException" %>
<%@ page import="jakarta.servlet.http.HttpSession" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="xyzboom" uri="com.xyzboom.tld" %>
<%
    HttpSession session1 = request.getSession();
    Page<Course> this_page = Page.cast(((Page<?>) session1.getAttribute("allCoursePage")));
    int pageNo = 1;
    String pageNoStr = request.getParameter("pageNo");
    try {
        pageNo = Integer.parseInt(pageNoStr);
    } catch (NumberFormatException ignored) {
    }
    if (this_page == null) {
        try {
            this_page = new Page<>(CourseDAO::getAllCourses,
                    CourseDAO::getAllCourseCount, Course.class);
        } catch (NotEntityClassException | EntityColumnException e) {
            out.println("页面创建失败");
            return;
        }
//     = new ArrayList<>();
//    try {
//        courses = CourseDAO.getAllCourses();
//    } catch (SQLException e) {
//        e.printStackTrace();
//        request.setAttribute("reason", "数据连接错误");
//        WebUtils.forward("/courseFailure.jsp", request, response);
//    }
        request.getSession().setAttribute("allCoursePage", this_page);
    }
    try {
        this_page.setPageNo(pageNo);
    } catch (Page.PageSizeOutOfBoundsException e) {
//        e.printStackTrace();
        out.println("没有需要的页码！");
        return;
    }
    String orderBy = request.getParameter("orderBy");
    this_page.setOrderBy(orderBy);
    String descend = request.getParameter("descend");
    this_page.setDescend(descend);
%>
<%
    Course.CourseType[] courseTypes = Course.CourseType.values();
    request.setAttribute("courseTypes", courseTypes);
%></>
<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css"
          href="<xyzboom:res type="css" name="css1"/>"/>
    <meta charset="UTF-8">
    <title>课程目录</title>
</head>
<body>
<h1>全部课程</h1>
<div>
    <form method="get" action="<c:url value="/servlets/course/CourseController"/>" id="form">
        <label>
            <input type="checkbox" name="hitCourse" style="display: none" id="hitCourse"/>
        </label>
        <div style="display: none" id="uarr">&uarr;</div>
        <div style="display: none" id="darr">&darr;</div>
        <table class="table_allCourse" cellspacing="0">
            <tr>
                <th>&nbsp;&nbsp;&nbsp;</th>
                <th>序号</th>
                <th onclick="onTableHeadClick(this,'cName')"
                    style="cursor:pointer;">课程名称
                    <c:if test="${param.orderBy.equals(\"cName\") && param.descend.equals(\"true\")}">
                        &darr;
                    </c:if>
                    <c:if test="${param.orderBy.equals(\"cName\") && !param.descend.equals(\"true\")}">
                        &uarr;
                    </c:if>
                </th>
                <th onclick="onTableHeadClick(this,'cNum')"
                    style="cursor:pointer;">选课人数
                    <c:if test="${param.orderBy.equals(\"cNum\") && param.descend.equals(\"true\")}">
                        &darr;
                    </c:if>
                    <c:if test="${param.orderBy.equals(\"cNum\") && !param.descend.equals(\"true\")}">
                        &uarr;
                    </c:if>
                </th>
                <th>课程性质</th>
            </tr>
            <c:forEach items="${sessionScope.allCoursePage.list}" var="c">
                <tr>
                    <td>
                        <label>
                            <input type="checkbox"
                                   name="${c.id}"/>
                        </label>
                    </td>
                    <td>
                            ${c.id}
                    </td>
                    <td>
                            ${c.name}
                    </td>
                    <td>
                            ${c.num}
                    </td>
                    <td>
                        <label>
                            <select>
                                <c:forEach items="${requestScope.courseTypes}" var="type">
                                    <option value=""
                                            <c:if test="${c.typeEquals(type)}">
                                                selected = selected
                                            </c:if>
                                    >
                                            ${type.type}
                                    </option>
                                </c:forEach>
                            </select>
                        </label>
                    </td>
                </tr>
            </c:forEach>
            <tr>
                <td colspan="5" style="text-align: center">
                    第${sessionScope.allCoursePage.pageNo}页&nbsp;
                    共${sessionScope.allCoursePage.totalPageSize}页
                </td>
            </tr>
            <tr>
                <td colspan="5" style="text-align: center">
                    <div id="pages">
                        &lt;<a href="allCourse.jsp?pageNo=${sessionScope.allCoursePage.prevPage}"
                            <c:if test="${sessionScope.allCoursePage.prevPage ==-1}">
                                style="cursor:pointer; pointer-events: none; color: dimgrey"
                            </c:if>
                    >前页</a>&nbsp;
                        <%-- 当前页码大于6小于总数减5时 --%>
                        <c:if test="${sessionScope.allCoursePage.totalPageSize > sessionScope.allCoursePage.pageNo + 5
         && sessionScope.allCoursePage.pageNo > 6 && sessionScope.allCoursePage.totalPageSize > 6}">
                            <a href="allCourse.jsp?pageNo=1">1</a>&nbsp;
                            <a href="allCourse.jsp?pageNo=1">2</a>&nbsp;...
                            <a href="allCourse.jsp?pageNo=${sessionScope.allCoursePage.prevPage}">
                                    ${sessionScope.allCoursePage.prevPage}</a>&nbsp;
                            ${sessionScope.allCoursePage.pageNo}&nbsp;
                            <a href="allCourse.jsp?pageNo=${sessionScope.allCoursePage.nextPage}">
                                    ${sessionScope.allCoursePage.nextPage}</a>&nbsp;...
                            <a href="allCourse.jsp?pageNo=${sessionScope.allCoursePage.totalPageSize-1}">
                                    ${sessionScope.allCoursePage.totalPageSize-1}</a>&nbsp;
                            <a href="allCourse.jsp?pageNo=${sessionScope.allCoursePage.totalPageSize}">
                                    ${sessionScope.allCoursePage.totalPageSize}</a>
                        </c:if>
                        <%-- 当前页码小于6时 --%>
                        <c:if test="${sessionScope.allCoursePage.pageNo <= 6}">
                            <xyzboom:for var="i" start="1" end="${xyzboom:min(sessionScope.allCoursePage.totalPageSize, 6)}">
                                <c:choose>
                                    <c:when test="${sessionScope.allCoursePage.pageNo == i}">
                                        ${sessionScope.allCoursePage.pageNo}&nbsp;
                                    </c:when>
                                    <c:when test="${sessionScope.allCoursePage.pageNo != i}">
                                        <a href="allCourse.jsp?pageNo=${i}">${i}</a>&nbsp;
                                    </c:when>
                                </c:choose>
                            </xyzboom:for>
                            <c:if test="${sessionScope.allCoursePage.totalPageSize > 6}">
                                ...&nbsp;
                                <a href="allCourse.jsp?pageNo=${sessionScope.allCoursePage.totalPageSize-1}">
                                        ${sessionScope.allCoursePage.totalPageSize-1}</a>&nbsp;
                                <a href="allCourse.jsp?pageNo=${sessionScope.allCoursePage.totalPageSize}">
                                        ${sessionScope.allCoursePage.totalPageSize}</a>
                            </c:if>
                        </c:if>
                        <%-- 当前页码大于总数减5时 --%>
                        <c:if test="${sessionScope.allCoursePage.totalPageSize
                     <= sessionScope.allCoursePage.pageNo + 5 && sessionScope.allCoursePage.totalPageSize > 6}">
                            <a href="allCourse.jsp?pageNo=1">1</a>&nbsp;
                            <a href="allCourse.jsp?pageNo=1">2</a>&nbsp;...
                            <xyzboom:for var="i" start="${sessionScope.allCoursePage.totalPageSize-5}"
                                         end="${sessionScope.allCoursePage.totalPageSize}">
                                <c:choose>
                                    <c:when test="${sessionScope.allCoursePage.pageNo == i}">
                                        ${sessionScope.allCoursePage.pageNo}&nbsp;
                                    </c:when>
                                    <c:when test="${sessionScope.allCoursePage.pageNo != i}">
                                        <a href="allCourse.jsp?pageNo=${i}">${i}</a>&nbsp;
                                    </c:when>
                                </c:choose>
                            </xyzboom:for>&nbsp;
                        </c:if>
                        <a href="allCourse.jsp?pageNo=${sessionScope.allCoursePage.nextPage}"
                                <c:if test="${sessionScope.allCoursePage.nextPage ==-1}">
                                    style="cursor:pointer; pointer-events: none; color: dimgrey"
                                </c:if>
                        >后页</a>&gt;
                    </div>
                </td>
            </tr>
        </table>
        <br/>
        <input type="submit" value="删除"/>
        <button onclick='return onAddCourse()'>新增</button>
        <input type="submit" value="选课" onclick="onHitCourse()"/>
    </form>
    <br/>
</div>
<script type="text/javascript" charset="UTF-8" src="<c:url value="/resource?other=/js/_utils/url.js"/>">
</script>
<script type="text/javascript" charset="UTF-8" src="<xyzboom:res type="js" name="js1"/>">
</script>
</body>
</html>